package com.baijiayun.liveuibase.toolbox

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.Toast
import com.baijiayun.livecore.context.LPConstants
import com.baijiayun.livecore.context.LiveRoom
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.base.RouterListener
import com.baijiayun.liveuibase.toolbox.rollcall.RollCallStatus
import com.baijiayun.liveuibase.utils.DisplayUtils.isPad
import com.baijiayun.liveuibase.utils.ThemeDataUtil
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder
import com.baijiayun.liveuibase.widgets.popupwindow.BaseAutoArrangePopupWindow
import java.util.*

/**
 * 工具箱
 * 顺序：打开网页-小黑板-计时器-答题器-抢答题-红包雨-点名-屏幕分享
 */
class ToolboxWindow @SuppressLint("ClickableViewAccessibility") constructor(
    context: Context
) : BaseAutoArrangePopupWindow(context) {

    private val routerListener by lazy {
        context as RouterListener
    }

    override fun initViewDataHashMap() {
        val groupViewDataHashMap = HashMap<Int, GroupViewData>()
        var lineWidth = 180
        var maxCount = 3
        if (getEnableCount() == 1) {
            lineWidth = 60
            maxCount = 1
        } else if (getEnableCount() == 2) {
            lineWidth = 120
            maxCount = 2
        }
        groupViewDataHashMap[ResSet.DEFAULT_GROUP_ID] =
            GroupViewData(context, lineWidth, 70, 50, 70, maxCount)
        viewDataHashMap = groupViewDataHashMap
    }

    override fun show(anchor: View) {
        val direction = if(routerListener.getLiveRoom()?.roomType == LPConstants.LPRoomType.NewSmallGroup){
            LEFT_BOTTOM
        } else {
            if (isPad(context)) RIGHT_BOTTOM else LEFT_BOTTOM
        }
        showWithViewOfDirection(anchor, direction)
    }

    private var clickListener: OnClickListener? = null
    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

    init {
        val resSets = arrayOf(
            ResSet(
                getDrawable(R.drawable.base_ic_browser),
                context.getString(R.string.bjy_base_open_page)
            ) {
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onBrowserClick()
                    dismiss()
                }
            },
            ResSet(
                getDrawable(R.drawable.base_ic_small_blackboard),
                context.getString(R.string.bjy_base_small_blackboard)
            ) {
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onSmallBlackboardClick()
                    dismiss()
                }
            },
            ResSet(
                getDrawable(R.drawable.base_ic_timer),
                context.getString(R.string.base_toolbox_timer)
            ) {
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onTimerClick()
                    dismiss()
                }
            },
            ResSet(
                getDrawable(R.drawable.base_ic_answerer),
                context.getString(R.string.base_toolbox_answerer)
            ) {
                if (routerListener.getLiveRoom()?.adminAuth?.answer != true) {
                    Toast.makeText(
                        context,
                        context.getString(R.string.bjy_base_live_answer_forbid),
                        Toast.LENGTH_LONG
                    ).show()
                    return@ResSet
                }
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onAnswererClick()
                    dismiss()
                }
            },
            ResSet(
                getDrawable(R.drawable.base_ic_responder),
                context.getString(R.string.base_responder)
            ) {
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onResponderClick()
                    dismiss()
                }
            },
            ResSet(
                getDrawable(R.drawable.base_ic_read_packet),
                context.getString(R.string.base_toolbox_red_packet)
            ) {
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onRedPacketClick()
                    dismiss()
                }
            },
            ResSet(
                getDrawable(R.drawable.base_ic_roll_call),
                context.getString(R.string.base_toolbox_roll_call), DrawableBuilder()
                    .solidColor(
                        ThemeDataUtil.getColorFromThemeConfigByAttrId(
                            context,
                            R.attr.base_theme_live_product_color
                        )
                    )
                    .oval()
                    .build()
            ) {
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onRollCallClick()
                    dismiss()
                }
            },ResSet(
                getDrawable(R.drawable.base_ic_screen_share),
                context.getString(R.string.bjy_base_toolbox_screen_share)
            ) {
                if (clickListener != null && clickListener!!.isClickable()) {
                    clickListener!!.onScreenShareClick()
                    dismiss()
                }
            })

        // !!!!! Warning!!!! 这里每加一个 Res 要在下边的 enable Array里相应的补充一个！！！
        val enableArray = getEnableArray(routerListener.getLiveRoom())
        for ((index, enable) in enableArray.withIndex()) {
            resSets[index].isEnable = enable
        }
        // 点名提示
        resSets[6].isShowDot = routerListener.getRollCallStatus() == RollCallStatus.Going
        // 大班课pad和手机方向不同
        if (routerListener.getLiveRoom()?.roomType != LPConstants.LPRoomType.NewSmallGroup) {
            val directionMode = if (isPad(context)) HORIZONTAL else VERTICAL
            setDirectionMode(directionMode)
        }
        setResSets(resSets)
        setContentPadding(0, 4, 0, 8)
        createView()
        // 工具箱默认获取焦点
        isFocusable = true
    }

    private fun getEnableCount(): Int {
        val enableArray = getEnableArray(routerListener.getLiveRoom())
        if (enableArray.isEmpty()) {
            return 0
        }
        var enableCount = 0
        for (enable in enableArray) {
            if (enable) {
                enableCount++
            }
        }
        return enableCount
    }

    companion object {
        fun getEnableArray(liveRoom: LiveRoom?): ArrayList<Boolean> {
            if (liveRoom == null) {
                return ArrayList<Boolean>()
            }
            val isTeacher =
                liveRoom.currentUser?.type == LPConstants.LPUserType.Teacher
            var isSmallGroup = false
            val roomType = if (liveRoom.roomType == LPConstants.LPRoomType.NewSmallGroup) {
                isSmallGroup = true
                if (liveRoom.smallClassTemplateType == LPConstants.SmallClassTemplateType.ONEONONE) {
                    RoomType.NewSmallGroupOneOnOne
                } else {
                    RoomType.NewSmallGroup
                }
            } else {
                isSmallGroup = false
                if (liveRoom.roomType == LPConstants.LPRoomType.OneOnOne) {
                    RoomType.MutilOneOnOne
                } else {
                    RoomType.Mutil
                }
            }
            val enableArray = ArrayList<Boolean>()
            if (isSmallGroup) {
                // 1v1 只有打开网页和计时器
                // 0 打开网页
                enableArray.add( liveRoom.partnerConfig.enableUseWebpage)
                // 1 小黑板 专业小班课只有老师有权限
                enableArray.add(isTeacher && roomType != RoomType.NewSmallGroupOneOnOne && liveRoom.partnerConfig.enableUseSnippet)
                // 2 计时器
                enableArray.add(liveRoom.partnerConfig.enableUseTimer)
                // 3 答题器
                enableArray.add(roomType != RoomType.NewSmallGroupOneOnOne && liveRoom.partnerConfig.enableUseAnswer)
                // 4 读取配置项且仅有老师和大班助教有抢答器
                enableArray.add(roomType != RoomType.NewSmallGroupOneOnOne && liveRoom.partnerConfig.enableUseRaceAnswer)
                // 5 红包雨
                enableArray.add(false)
                // 6 点名
                enableArray.add(roomType != RoomType.NewSmallGroupOneOnOne)

            } else {
                // 0 打开网页
                enableArray.add(false)
                // 1 小黑板
                enableArray.add(false)
                // 2 仅老师有计时器
                enableArray.add(liveRoom.currentUser.type == LPConstants.LPUserType.Teacher)
                // 3 1v1 都没有答题器
                enableArray.add(liveRoom.roomType != LPConstants.LPRoomType.OneOnOne)
                // 4 读取配置项且仅有老师和大班助教有抢答器
                enableArray.add(
                    liveRoom.partnerConfig.enableUseRaceAnswer
                            && liveRoom.isTeacherOrAssistant
                            && liveRoom.roomType != LPConstants.LPRoomType.OneOnOne
                )
                // 5 仅老师且非1v1 有红包雨
                enableArray.add(
                    liveRoom.partnerConfig.enableRedPacket == 1
                            && liveRoom.currentUser.type == LPConstants.LPUserType.Teacher
                            && liveRoom.roomType != LPConstants.LPRoomType.OneOnOne
                )
                // 6 仅老师且非1v1 有点名
                enableArray.add(liveRoom.roomType != LPConstants.LPRoomType.OneOnOne)
            }
            // 7屏幕共享
            enableArray.add(false)
//            enableArray.add(liveRoom.enableScreenShare())
            return enableArray
        }
    }
    enum class RoomType{
        Mutil,MutilOneOnOne,NewSmallGroup,NewSmallGroupOneOnOne
    }

    interface OnClickListener {
        fun isClickable(): Boolean
        fun onBrowserClick()
        fun onSmallBlackboardClick()
        fun onTimerClick()
        fun onAnswererClick()
        fun onResponderClick()
        fun onRedPacketClick()
        fun onRollCallClick()
        fun onScreenShareClick()
    }
}